#include "common.h"
#include "gshader.h"

CGShader::CGShader(shader_type type)
{
	m_nType = type;
	m_pShaderHandle = NULL;
}

CGShader::~CGShader(void)
{
}

bool CGShader::Load(const std::string& szSource)
{
	m_szSource = szSource;
	const char * pSourceCopy = m_szSource.c_str();
	if (m_nType == fragment_shader)
		m_pShaderHandle = glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB);
	else if (m_nType == vertex_shader)
		m_pShaderHandle = glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB);
	else return false;

	glShaderSourceARB(m_pShaderHandle, 1, &pSourceCopy, NULL);
	return true;
}
bool CGShader::LoadFromFile(const char* pFileName)
{
	std::string szBody;

	FILE * pFile = fopen(pFileName, "r");
	if (!pFile)
		return false;
	char buffer[1024];
	while (fgets(buffer, 1024, pFile) != NULL)
	{
		szBody += buffer;
		szBody += "\n";
	}
	fclose(pFile);

	return Load(szBody);
}

bool CGShader::CompileLink(GLhandleARB pProgramID)
{
	glCompileShaderARB(m_pShaderHandle);
	glAttachObjectARB(pProgramID, m_pShaderHandle);
	return true;
}

bool CGShader::AttachObject(GLhandleARB pObjectID)
{
	glAttachObjectARB(pObjectID, m_pShaderHandle);
	return true;
}